﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Tools;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Environment;

namespace Pixysoft.Weblications.Sango.Formular
{
    class ResourceFormular
    {
        public const int default_max_harvestcount = 3;

        /// <summary>
        /// 资源田产量
        /// </summary>
        /// <param name="resourceLevel"></param>
        /// <returns></returns>
        public static long GetResourceProductivityValue(long resourceLevel)
        {
            //良田产量在100级内的区间是 = [672, 30240]， 得到295.68x + 672，x = 玩家等级，实际中使用地区的级别。

            return (long)Math.Round(295.68 * resourceLevel + 672f);
        }

        /// <summary>
        /// 资源是否需要结算
        /// </summary>
        /// <param name="resource"></param>
        /// <returns></returns>
        public static bool resourceNeedBalance(Pixysoft.Weblications.Sango.Entities.Usr_Resource resource)
        {
            if (resource.Enddate < GlobalTimer.Instance.ToTimeSequence())
                return true;

            return false;
        }

        /// <summary>
        /// 玩家能够占有最大的资源田数目 目前为1
        /// </summary>
        /// <param name="profile"></param>
        /// <returns></returns>
        public static int GetMaxOccupyResourceCount(Pixysoft.Weblications.Sango.Entities.Usr_Profile profile)
        {
            return 1;
        }

        /// <summary>
        /// 资源田结算
        /// </summary>
        /// <param name="resource"></param>
        /// <param name="profile"></param>
        public static void resourceCurrentBalance(Entities.Usr_Resource resource, Entities.Usr_Profile profile)
        {
            Entities.Cti_Resource cityResource = CtiResourceHelper.searchByCode(resource.Resourceserialcode);

            long resourcevalue = GetResourceProductivityValue(cityResource.Resourcelevel);

            double timespan = resource.Enddate - resource.Startdate;

            double factor = timespan / GetResourceTimespan();

            resourcevalue = (long)Math.Round(resourcevalue * factor);

            UsrProfileDao.increateResourceValueToMax(profile, resourcevalue);

            resource.Enddate = PlayerFormular.default_number_empty;

            resource.Status = GeneralStatusCollection.CLOSE;
        }

        /// <summary>
        /// 资源抢夺成功，获取的部分奖励 = 1/10
        /// </summary>
        /// <param name="resource"></param>
        /// <param name="profile"></param>
        public static void resourceFightBonus(Entities.Usr_Resource resource, Entities.Usr_Profile profile)
        {
            Entities.Cti_Resource cityResource = CtiResourceHelper.searchByCode(resource.Resourceserialcode);

            long resourceValue = (long)Math.Round(GetResourceProductivityValue(cityResource.Resourcelevel) / 10f);

            UsrProfileDao.increateResourceValueToMax(profile, resourceValue);
        }

        /// <summary>
        /// 资源帮助收割 双方都能获得部分奖励
        /// </summary>
        /// <param name="resource"></param>
        /// <param name="profile"></param>
        /// <param name="targetprofile"></param>
        public static void resourceHarvest(Entities.Usr_Resource resource, Entities.Usr_Profile profile, Entities.Usr_Profile targetprofile)
        {
            resourceFightBonus(resource, profile);

            resourceFightBonus(resource, targetprofile);
        }

        /// <summary>
        /// 资源田占有时间
        /// </summary>
        /// <returns></returns>
        public static double GetResourceTimespan()
        {
            return GlobalTimer.Instance.ToTimeSequence() + DateTimeCoder.Instance.HourToMilliSecond(4);
        }

        /// <summary>
        /// 资源收获时间刷新 每2小时一次
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public static bool IsHarvestRefresh(double lastdate)
        {
            //找出比现在时间大的最近2小时

            DateTime currentdate = GlobalTimer.Instance.GetGlobalTime();

            DateTime headofday = DateTimeCoder.Instance.GetHeadOfToday();

            while (true)
            {
                if (headofday.AddHours(2) > currentdate)
                    break;

                headofday = headofday.AddHours(2);
            }

            return SettingHelper.IsRefresh(GlobalTimer.Instance.ToTimeSequence(headofday), lastdate, DateTimeCoder.Instance.HourToMilliSecond(2));
        }
    }
}
